(function() {
	
	var appModule = angular.module('tracecashModule');
	
	appModule.directive('companyField', function() {
		return {
			restrict : 'AE',
			template : '<input type="text" ng-model="ngModel" ng-disabled="disabled" typeahead="company as (company.code + \' - \' + company.name) for company in getCompany($viewValue)" typeahead-loading="loadingCompanies" class="form-control"><i ng-show="loadingCompanies"><span class="glyphicon glyphicon-refresh spin"></span></i>',
			controller : ['$scope', '$rootScope', '$http', function($scope, $rootScope, $http) {
				$scope.selected = undefined;
				$scope.getCompany = function(val) {
					return $http.get($rootScope.REST_PATH + '/company/like/' + val).then(function(response) {
						return response.data.map(function(item){
							return item;
				        });
					});
				};
			}],
			require : '^ngModel',
			scope : {
				ngModel : '=',
				disabled : '@'
			}
		};
	});
	
	appModule.directive('subcompanyField', function() {
		return {
			restrict : 'AE',
			template : '<input type="text" ng-model="ngModel" ng-disabled="disabled" typeahead="subcompany as subcompany.name for subcompany in getSubcompany($viewValue)" typeahead-loading="loadingSubcompanies" class="form-control"><i ng-show="loadingSubcompanies"><span class="glyphicon glyphicon-refresh spin"></span></i>',
			controller : ['$scope', '$rootScope', '$http', function($scope, $rootScope, $http) {
				$scope.selected = undefined;
				$scope.getSubcompany = function(val) {
					return $http.get($rootScope.REST_PATH + '/subcompany/like/' + val, {
						params: {
							companyId : $scope.companyId
						}
					}).then(function(response) {
						return response.data.map(function(item){
							return item;
				        });
					});
				};
			}],
			require : '^ngModel',
			scope : {
				ngModel : '=',
				companyId : '=',
				disabled : '@'
			}
		};
	});
	
	appModule.directive('networkField', function() {
		return {
			restrict : 'AE',
			template : '<input type="text" ng-model="ngModel" ng-disabled="disabled" typeahead="network as (network.code + \' - \' + network.description) for network in getNetwork($viewValue)" typeahead-loading="loadingNetworks" class="form-control"><i ng-show="loadingNetworks"><span class="glyphicon glyphicon-refresh spin"></span></i>',
			controller : ['$scope', '$rootScope', '$http', function($scope, $rootScope, $http) {
				$scope.selected = undefined;
				$scope.getNetwork = function(val) {
					return $http.get($rootScope.REST_PATH + '/network/like/' + val, {
						params : {
							companyId : $scope.companyId,
							subcompanyId : $scope.subcompanyId 
						}
					}).then(function(response) {
						return response.data.map(function(item){
							return item;
				        });
					});
				};
			}],
			require : '^ngModel',
			scope : {
				ngModel : '=',
				companyId : '=',
				subcompanyId : '=',
				disabled : '@'
			}
		};
	});

	appModule.directive('agreementField', function() {
		return {
			restrict : 'AE',
			template : '<input type="text" ng-model="ngModel" ng-disabled="disabled" typeahead="agreement as (agreement.code + \' - \' + agreement.name + \' - \' + agreement.description) for agreement in getAgreement($viewValue)" typeahead-loading="loadingAgreements" class="form-control"><i ng-show="loadingAgreements"><span class="glyphicon glyphicon-refresh spin"></span></i>',
			controller : ['$scope', '$rootScope', '$http', function($scope, $rootScope, $http) {
				$scope.selected = undefined;
				$scope.getAgreement = function(val) {
					return $http.get($rootScope.REST_PATH + '/agreement/like/' + val).then(function(response) {
						return response.data.map(function(item){
							return item;
				        });
					});
				};
			}],
			require : '^ngModel',
			scope : {
				ngModel : '=',
				disabled : '@'
			}
		};
	});
	
	appModule.directive('warrantyTypeField', function() {
		return {
			restrict : 'AE',
			template : '<input type="text" ng-model="ngModel" ng-disabled="disabled" typeahead="warrantyType as (warrantyType.code + \' - \' + (warrantyType.description | translate)) for warrantyType in getWarrantyType($viewValue)" typeahead-loading="loadingWarrantyTypes" class="form-control"><i ng-show="loadingWarrantyTypes"><span class="glyphicon glyphicon-refresh spin"></span></i>',
			controller : ['$scope', '$rootScope', '$http', function($scope, $rootScope, $http) {
				$scope.selected = undefined;
				$scope.getWarrantyType = function(val) {
					return $http.get($rootScope.REST_PATH + '/warrantyType/like/' + val).then(function(response) {
						return response.data.map(function(item){
							return item;
				        });
					});
				};
			}],
			require : '^ngModel',
			scope : {
				ngModel : '=',
				disabled : '@'
			}
		};
	});
	
	appModule.directive('warrantySubtypeField', function() {
		return {
			restrict : 'AE',
			templateUrl : 'html/components/select-enum.html',
			require : ['^ngModel'],
			controller : ['$scope', '$rootScope', '$http', function($scope, $rootScope, $http) {
				$scope.enumName = 'WarrantySubtype';
				$http.get($rootScope.REST_PATH + '/warrantyType/subtype/' + $scope.warrantyCode, {}).success(function(data) {
					$scope.names = data;
				});
			}],
			scope : {
				ngModel : '=',
				ngDisabled : '=',
				warrantyCode : '='
			}
		};
	});
	
})();